//
//  LinkingCardResultAlert.swift
//  iPhone
//
//  Created by Toj on 11/8/22.
//

import UIKit

enum LinkingCardResultStatus: Int {
    case none
    case resultSuccess
    case resultFaild
    case tips
    case login
    
    var size: CGSize {
        switch self {
        case .resultSuccess, .resultFaild:
            return CGSize(width: 280.ui_mscale, height: 220.ui_scale)
        case .tips:
            return CGSize(width: 280.ui_mscale, height: 200.ui_scale)
        default:
            break
        }
        return .zero
    }
}

class LinkingCardResultAlert: UIView {
    
    var status: LinkingCardResultStatus = .none { didSet {
        var size = status.size
        switch status {
        case .resultSuccess:
            resultView.state = .success
            contentView = resultView
        case .resultFaild:
            resultView.state = .faild
            contentView = resultView
        case .tips:
            contentView = tipsView
        case .login:
            let phoneNumber = "+86 134 1234 1234"
            let decoderNumber = arc4random_uniform(2) == 1 ? "234578" : ""
            loginView.phone = phoneNumber
            loginView.decoder = decoderNumber
            contentView = loginView
            let maxWidth = 280.ui_mscale - 16.ui_scale
            let phoneHeight = loginView.phoneLabel.attributedText?.string.cm_stringHeight(font: UIFont.boldSystemFont(ofSize: 14), maxWidth: maxWidth) ?? 0
            var decoderHeight: CGFloat = 0
            if let attributedText = loginView.decoderLabel.attributedText {
                decoderHeight = attributedText.string.cm_stringHeight(font: UIFont.boldSystemFont(ofSize: 11), maxWidth: maxWidth) + 12 // 12 = 间距
            }
            
            // height = 24 + phone (+ 12 + decoder) + 20 + 32 + 20 + 32 + 40
            let height = phoneHeight + decoderHeight + 148.ui_scale
            size = CGSize(width: maxWidth, height: height)
        default:
            break
        }
        contentView.frame.size = size
        addSubview(contentView)
        setNeedsLayout()
    } }
    
    @objc private func didTapOK() {
        // print("-->toj: " + #function)
        removeFromSuperview()
    }
    
    @objc private func didTapUnlogin() {
        // print("-->toj: " + #function)
        removeFromSuperview()
    }
    
    @objc private func didTapLogin() {
        // print("-->toj: " + #function)
        removeFromSuperview()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupLayoutSubviews()
    }
    
    private lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        return view
    }()
    
    private let cornerRadius: CGFloat = 8
    private var contentView: UIView = UIView() { didSet {
        contentView.backgroundColor = .white
        contentView.cm_cornerRadius = cornerRadius
    } }
    
    private lazy var shadowView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.cm_shadow(color: .black, opacity: 0.3, radius: 24, corner: cornerRadius)
        return view
    }()
    
    private lazy var resultView: LinkingCardResultView = {
        let view = LinkingCardResultView()
        view.okButton.addTarget(self, action: #selector(didTapOK), for: .touchUpInside)
        return view
    }()
    
    private lazy var tipsView: LinkingCardTipsView = {
        let view = LinkingCardTipsView()
        view.okButton.addTarget(self, action: #selector(didTapOK), for: .touchUpInside)
        return view
    }()
    
    private lazy var loginView: LinkingCardLoginView = {
        let view = LinkingCardLoginView()
        view.notLoginButton.addTarget(self, action: #selector(didTapUnlogin), for: .touchUpInside)
        view.loginButton.addTarget(self, action: #selector(didTapLogin), for: .touchUpInside)
        return view
    }()
    
    deinit {
        // print("-->toj: LinkingCardResultAlert " + #function)
    }
}

extension LinkingCardResultAlert {
    
    private func setupAddSubviews() {
        
        addSubview(backgroundView)
        addSubview(shadowView)
        addSubview(contentView)
    }
    
    private func setupLayoutSubviews() {
        
        backgroundView.snp.remakeConstraints { make in
            make.edges.equalTo(self)
        }
        
        let size = contentView.frame.size
        contentView.snp.remakeConstraints { make in
            make.center.equalTo(self)
            make.width.equalTo(size.width)
            make.height.equalTo(size.height)
        }
        
        shadowView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
}
